package client;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import common.Utilities;

public class SysTray implements java.io.Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final int TRAY_SIZE = getTraySize();
	public static final String OFFLINE_TRAY_ICON = "resource/"+TRAY_SIZE+"/tray-offline.png";
	public static final String OONLINE_TRAY_ICON = "resource/"+TRAY_SIZE+"/tray-online.png";
	private IMClientGUI gui;
	private TrayIcon trayIcon;
	
	public SysTray(IMClientGUI gui) {
		this.gui = gui;
        createAndShowGUI();
    }

	private static int getTraySize() {
		if(SystemTray.isSupported()){
			int size = SystemTray.getSystemTray().getTrayIconSize().width;
			if(size <= 19) size = 16;
			else if(size>19 && size<=27) size = 22;
			else if(size>27 && size<=40) size = 32;
			if(size > 40) size =48;
			return size;
		}
		return 48;
	}

	public TrayIcon getTrayIcon(){
		return trayIcon;
	}
	
    private void createAndShowGUI() {
        //Check the SystemTray support
        if (!SystemTray.isSupported()) {
            System.out.println("SystemTray is not supported");
            return;
        }
        final PopupMenu popup = new PopupMenu();
        trayIcon =
                new TrayIcon(Utilities.createImage(OFFLINE_TRAY_ICON, "tray icon"));
        final SystemTray tray = SystemTray.getSystemTray();
        trayIcon.setImageAutoSize(true);
        trayIcon.setToolTip("My IM Client");
        
        
        // Create a popup menu components
        MenuItem aboutItem = new MenuItem("About");
        MenuItem showItem = new MenuItem("Show");
        MenuItem hideItem = new MenuItem("Hide");
        MenuItem exitItem = new MenuItem("Exit");
        
        //Add components to popup menu
        popup.add(aboutItem);
        popup.addSeparator();
        popup.add(showItem);
        popup.add(hideItem);
        popup.addSeparator();
        popup.add(exitItem);
        
        trayIcon.setPopupMenu(popup);
        
        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            System.out.println("TrayIcon could not be added.");
            return;
        }
        
        trayIcon.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	gui.setVisible(true);
            	gui.toFront();
            }
        });
        
        aboutItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null,
                        "My IM Client By Zexu Xu");
            }
        });
        
        showItem.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		gui.setVisible(true);
        		gui.toFront();
        	}
        });
        
        hideItem.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		gui.setVisible(false);
        	}
        });
        
        exitItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	gui.logout(false);
            	gui.dispose();
                tray.remove(trayIcon);
                System.exit(0);
            }
        });
    }

}
